﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using FinancialAnalytic.BusinessLogic;

using System.IO;
using System.Xml;
using FinancialAnalytic.BusinessLogic.DataEngine;
using System.Configuration;

namespace FinancialAnalytic.ProjectFinance
{
    public partial class MainForm : Form
    {
        private string configurationSettingpath = string.Empty;
        private FinancialAnalyticConfiguration settings = new FinancialAnalyticConfiguration();

        public MainForm()
        {
            InitializeComponent();
        }

        private void addStocksToListsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void dlData_btn_Click(object sender, EventArgs e)
        {
            m_StockInfoList.Items.Clear();
            DisplayMarketData();
           // DisplaySecuritiesList();
            
        }


        private void DisplayMarketData()
        {
            string[] dow = {"MMM", "AA", "MO", "AXP", "AIG", "T ", "BA", "CAT", "C", "KO", "DD", "XOM", "GE", "GM", "HPQ", "HD", "HON", "INTC", "IBM", "JNJ", "JPM", "MCD", "MRK", "MSFT", "PFE", "PG", "UTX", "VZ", "WMT", "DIS" };
            List<string> stocklist = new List<string>();
            stocklist.AddRange(dow);

            PopulateListView(stocklist);

        }

        private void DisplaySecuritiesList()
        {
            DataTable dt = new DataTable();
            StockQuoteDownloader ds = new StockQuoteDownloader();
            dt = ds.YahooData("IBM",DateTime.Now.AddDays(-800),DateTime.Now.AddDays(-1));

            foreach (DataRow dc in dt.Rows)
            {
                ListViewItem lvl = new ListViewItem(dc[0].ToString());
                lvl.SubItems.Add(dc[1].ToString());
                lvl.SubItems.Add(dc[2].ToString());
                lvl.SubItems.Add(dc[3].ToString());
                lvl.SubItems.Add(dc[4].ToString());
                lvl.SubItems.Add(dc[5].ToString());
                lvl.SubItems.Add(dc[6].ToString());

                m_StockInfoList.Items.Add(lvl);

            }

        }

        private void PopulateListView(List<string> items)
        {


            StocksData sd = new StocksData();
            MarketDataDownloader mktdl = new MarketDataDownloader();


            foreach (string stock in items)
            {
                sd = mktdl.GetStockData(stock);
                ListViewItem lvl = new ListViewItem(sd.Symbol.ToString());

                lvl.UseItemStyleForSubItems = false;

                lvl.SubItems.Add(sd.PreviousClose.ToString());
                lvl.SubItems.Add(sd.Last.ToString());
                lvl.SubItems.Add(sd.High.ToString());
                lvl.SubItems.Add(sd.Low.ToString());
                lvl.SubItems.Add(sd.Volume.ToString());                
                lvl.SubItems.Add(sd.Change.ToString());
                lvl.SubItems.Add(sd.PctChange.ToString());          
                lvl.SubItems.Add(sd.MarketCapital.ToString());
                lvl.SubItems.Add(sd.PERatio.ToString());

                ChangeListViewFontColor(lvl,sd);

                m_StockInfoList.Items.Add(lvl);

            }


        }

        private void ChangeListViewFontColor(ListViewItem lvl, StocksData sd)
        {
            // Column Change Column
            if (sd.Change.Contains("-"))
            {
                lvl.SubItems[6].ForeColor = Color.Red;
            }
            else if (sd.Change.Contains("+"))
            {
                lvl.SubItems[6].ForeColor = Color.Green;
            }

            // Column Percentage Change Column
            if (sd.PctChange.Contains("-"))
            {
                lvl.SubItems[7].ForeColor = Color.Red;
            }
            else if (sd.PctChange.Contains("+"))
            {
                lvl.SubItems[7].ForeColor = Color.Green;
            }

        }

        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogbox = new OpenFileDialog();
            DialogResult result = dialogbox.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                
                string file = dialogbox.FileName;
                try
                {
                    FileInfo fileinfo = new FileInfo(file);
                    string text = File.ReadAllText(file);
                    
                }
                catch (IOException)
                {
                }
            }
        }

        private void openfile_btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogbox = new OpenFileDialog();
            DialogResult result = dialogbox.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {

                string file = dialogbox.FileName;
                try
                {
                    FileInfo fileinfo = new FileInfo(file);
                    string text = File.ReadAllText(file);

                }
                catch (IOException)
                {
                }
            }
        }

        private void print_form_btn_Click(object sender, EventArgs e)
        {

            Guid guid_number = Guid.NewGuid();
            string inputpath;
            string outputpath;
            string xmlstring;
            string templatepath;
            string msg;
            XmlDocument xmldoc = new XmlDocument();

            LoadConfiguration();

            inputpath = "C:\\analytic_crystalreport\\Resources\\input.xml";
            //templatepath = "C:\\analytic_crystalreport\\FinancialAnalytic.BusinessLogic\\Forms\\CrystalReport1.rpt";

            templatepath = settings.Reportpath;

            outputpath = "C:\\analytic_crystalreport\\Output\\" + guid_number.ToString() + ".pdf";
            

            XmlTextReader reader = new XmlTextReader(inputpath);
            xmldoc.Load(reader);

            xmlstring = xmldoc.InnerXml;

            msg = LinqTransformation.Transform(xmlstring);

            PDFGenerator.GenerateReport(msg, templatepath, outputpath);
            
        }

        private void LoadConfiguration()
        {
            configurationSettingpath = ConfigurationManager.AppSettings["configfile"].ToString();
            settings = (FinancialAnalyticConfiguration)Serializer.Deserialize(typeof(FinancialAnalyticConfiguration), configurationSettingpath);
        }

    }
}
